package com.lemon.smartsafetynew.ui.adapter.me;

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lemon.smartsafety.R;
import com.lemon.smartsafety.app.AppService;
import com.lemon.smartsafety.ui.adapter.common.RcvViewType;
import com.lemon.smartsafetynew.entity.event.me.MeUIEvent;
import com.lemon.smartsafetynew.entity.me.MePanelLabel;
import com.lemon.smartsafetynew.entity.me.MeRcvItemBase;

import java.util.List;

import kale.adapter.LemonRcvAdapter;
import kale.adapter.item.AdapterItem;

/**
 * Created by Andrew on 2017/7/4.
 */

public class MeOptionItemViewRec implements AdapterItem<MeRcvItemBase<List<MePanelLabel>>> {

    private RecyclerView mRecyclerView;

    private MeOptionItemAdapter mAdapter;

    /**
     * @return item布局文件的layoutId
     */
    @Override
    public int getLayoutResId() {
        return R.layout.view_recycler_view;
    }

    /**
     * 初始化views
     */
    @Override
    public void bindViews(View root) {
        mRecyclerView = (RecyclerView) root.findViewById(R.id.recyclerView);
    }

    /**
     * 设置view的参数
     */
    @Override
    public void setViews() {

    }

    /**
     * 根据数据来设置item的内部views
     *
     * @param itemBase 数据list内部的model
     * @param position 当前adapter调用item的位置
     */
    @Override
    public void handleData(MeRcvItemBase<List<MePanelLabel>>itemBase, int position) {

        if (RcvViewType.MeRcvViewType.TYPE_GROUP_1_OPTION != itemBase.getViewType()
                && RcvViewType.MeRcvViewType.TYPE_GROUP_2_OPTION != itemBase.getViewType() ) {
            return;
        }

        final List<MePanelLabel> items = itemBase.getData();
        LinearLayoutManager manager = new LinearLayoutManager(mRecyclerView.getContext());
        mAdapter = new MeOptionItemAdapter(items);
        mRecyclerView.setLayoutManager(manager);
        mRecyclerView.setAdapter(mAdapter);
    }

    public class MeOptionItemAdapter extends LemonRcvAdapter<MePanelLabel> {


        public MeOptionItemAdapter(@Nullable List<MePanelLabel> data) {
            super(data);
        }


        @Override
        public int getItemCount() {
            return getData().size();
        }

        @NonNull
        @Override
        public AdapterItem createItem(Object type) {
            return new MeOptionItemRecItem();
        }

        public class MeOptionItemRecItem implements AdapterItem<MePanelLabel> {

            LinearLayout layout;
            ImageView iconImage;
            TextView textView;

            private int mPosition;
            private MePanelLabel mData;

            @Override
            public int getLayoutResId() {
                return R.layout.item_secondary_panel;
            }

            @Override
            public void bindViews(View root) {
                layout = (LinearLayout) root.findViewById(R.id.secondary_option_layout);
                iconImage = (ImageView) root.findViewById(R.id.label_icon);
                textView = (TextView) root.findViewById(R.id.label_name);
            }


            @Override
            public void setViews() {
                layout.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        AppService.getBus().post(new MeUIEvent(mData));
                    }
                });

            }

            @Override
            public void handleData(MePanelLabel data, int position) {

                mPosition = position;
                mData = data;

                iconImage.setImageResource(data.getIconRes());
                textView.setText(data.getTitleRes());

            }
        }
    }
}
